<?php
// $Id: views_plugin_row.inc,v 1.1 2008/09/03 19:21:30 merlinofchaos Exp $
/**
 * @file
 * Contains the base row style plugin.
 */

/**
 * Default plugin to view a single row of a table. This is really just a wrapper around
 * a theme function.
 *
 * @ingroup views_row_plugins
 */
class views_plugin_row_tag extends views_plugin_row {
  /**
   * Initialize the row plugin.
   */
  function init(&$view, &$display, $options = NULL) {
    $this->view = &$view;
    $this->display = &$display;

    // Overlay incoming options on top of defaults
    $this->unpack_options($this->options, isset($options) ? $options : $display->handler->get_option('row_options'));
  }

  function uses_fields() {
    return !empty($this->definition['uses fields']);
  }

  function option_definition() {
    $options = parent::option_definition();
    
    $options['tag_name'] = array('default' => '', 'translatable' => TRUE);

    $options['fields_options'] = array(
      'default' => array(), 'translatable' => TRUE,
    );

    $options['root_value'] = array('default' => '', 'translatable' => FALSE, );

    return $options;
  }

  /**
   * Provide a form for setting options.
   */
  function options_form(&$form, &$form_state) {
    $form['tag_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Tag Name'),
      '#description' => t('The tag or the attribute name.'),
      '#default_value' => $this->options['tag_name'],
    );

    $parent_options = array('' => 'None');
    
    foreach($this->display->handler->get_handlers('field') as $key => $field) {
      $parent_options[$key] = $field->definition['group'] .': '. $field->definition['title'];
    }

    $form['root_value'] = array(
      '#type' => 'radios',
      '#title' => t('Root value'),
      '#options' => $parent_options,
      '#default_value' => $this->options['root_value'],
      '#description' => t('If you choose a field to be the root valeu all the other fields will be treated as a attribute'),
    );

    foreach($this->display->handler->get_handlers('field') as $key => $field) {
      $form['fields_options'][$key] = array(
        '#type' => 'fieldset',
        '#title' => $field->definition['group'] .': '. $field->definition['title'],
      );

      $form['fields_options'][$key]['is_attribute'] = array(
        '#type' => 'checkbox',
        '#title' => t('Is Attribute?'),
        '#description' => t('If this tag should be rendered of an attribute of its parent or as tag.'),
        '#default_value' => $this->options['fields_options'][$key]['is_attribute'],
      );

      $form['fields_options'][$key]['tag_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Tag Name'),
        '#description' => t('The tag or the attribute name.'),
        '#default_value' => $this->options['fields_options'][$key]['tag_name'],
      );

      $form['fields_options'][$key]['parent'] = array(
        '#type' => 'select',
        '#options' => $parent_options,
        '#title' => t('Parent tag'),
        '#description' => t('If you selected a parent for this field the value of the "Is Attribute?" '.
          'field will be ignored and this field will be attribute of its parent.'),
        '#default_value' => $this->options['fields_options'][$key]['parent'],
      );

      unset($form['fields_options'][$key]['parent']['#options'][$key]);
    }
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
  function options_submit($form, &$form_state) {
    /*foreach ($form_state['values']['row_options']['fields_options'] as $key => $field) {
      $this->options['fields_options'][$key]['is_attribute'] = 
        $form_state['values']['row_options']['fields_options'][$key]['is_attribute'];
      $this->options['fields_options'][$key]['tag_name'] = 
        $form_state['values']['row_options']['fields_options'][$key]['tag_name'];
    }*/
  }

  /**
   * Allow the style to do stuff before each row is rendered.
   *
   * @param $result
   *   The full array of results from the query.
   */
  function pre_render($result) { }

  /**
   * Render a row object. This usually passes through to a theme template
   * of some form, but not always.
   */
  function render($row) {
    return theme($this->theme_functions(), $this->view, $this->options, $row);
  }
}
